//------------------------------------------------------------------------------
//  characternode.cc
//  (C) 2008 Radon Labs GmbH
//------------------------------------------------------------------------------
#include "stdneb.h"
#include "characternode.h"

namespace Characters
{
__ImplementClass(Characters::CharacterNode, 'CHRN', Models::TransformNode);

using namespace Util;
using namespace IO;


//------------------------------------------------------------------------------
/**
*/
CharacterNode::CharacterNode()
{
    // empty
}

//------------------------------------------------------------------------------
/**
*/
CharacterNode::~CharacterNode()
{
}

////------------------------------------------------------------------------------
///**
//*/
//const CharacterJoint* CharacterNode::GetJoint( IndexT index)
//{
//	Util::Array<COLLADAFW::UniqueId> keys = this->jointMaps.KeysAsArray();
//	for (int i =0; i< keys.Size();i++ )
//	{
//		if( this->jointMaps[keys[i]].GetJointIndex() == index)
//		{
//			return &this->jointMaps[keys[i]];
//		}
//	}
//	return NULL;
//}
//
////------------------------------------------------------------------------------
///**
//*/
//bool CharacterNode::HasJoint( IndexT index)
//{
//	Util::Array<Characters::CharacterJoint> joints = this->jointMaps.ValuesAsArray();
//	for (int i =0; i< joints.Size();i++ )
//	{
//		if( joints[i].GetJointIndex() == index)
//			return true;
//	}
//	return false;
//}

//------------------------------------------------------------------------------
/**
*/
bool
CharacterNode::WriteDataTag(Ptr<ToolkitUtil::ModelWriter>& writer)
{
	if(TransformNode::WriteDataTag(writer))
	{
		if( animResId.IsValid())
		{
			writer->BeginTag("Animation", FourCC('ANIM'));
			writer->WriteString(this->animResId);
			writer->EndTag();
		}		

		if( variationsResId.IsValid())
		{
			writer->BeginTag("Variation", FourCC('VART'));
			writer->WriteString(this->variationsResId);
			writer->EndTag();
		}		



		//for (IndexT index = 0 ; index < this->jointArray.Size();index++ )
		//{
		//	writer->BeginTag("Joint", 'JONT');
		//	writer->WriteInt(this->jointArray[index].jointIndex);
		//	writer->WriteInt(this->jointArray[index].parentJointIndex);
		//	writer->WriteFloat4(this->jointArray[index].poseTranslation);
		//	writer->WriteFloat4(this->jointArray[index].poseRotation);
		//	writer->WriteFloat4(this->jointArray[index].poseScale);
		//	writer->WriteString(this->jointArray[index].jointName);
		//	writer->EndTag();
		//}

		const Util::Array<CharacterJoint>& jointList = this->jointMaps.ValuesAsArray();
		if( !jointList.IsEmpty())
		{
			writer->BeginTag("NumJoints", 'NJNT');
			writer->WriteInt(jointList.Size());
			writer->EndTag();


			for (IndexT index=0; index < jointList.Size();index++ )
			{
				jointList[index].WriteDataTag(writer);
			}/// end for
		}

		if( !skinList.IsEmpty())
		{
			writer->BeginTag("NumSkinLists", 'NSKL');
			writer->WriteInt(1);
			writer->EndTag();
			
			writer->BeginTag("SkinList", 'SKNL');
			String skinName = this->GetName().AsString();
			skinName.Append("_skin");
			writer->WriteString(skinName);
			writer->WriteInt( skinList.Size() );
			for(int i = 0; i< skinList.Size(); i++ )
			{
				writer->WriteString( skinList[i] );
			}/// end for
			writer->EndTag();
		}		
	}

	return false;
}

} // namespace Characters